﻿using LeaderCCSLaserUI.Data;
using LeaderCCSLaserUI.Data.Extensions;
using Newtonsoft.Json;
using NLog;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ModuleLeaderMES.ViewModels
{
    public class ViewAViewModel : NavigationViewModel
    {
        #region 变量
        string partNum;
        private static Logger logger = LogManager.GetCurrentClassLogger();
        private RestClient client;
        #endregion
        #region 属性
        private int lineCode;
        public int LineCode
        {
            get { return lineCode; }
            set { SetProperty(ref lineCode, value); }
        }
        private int machineCode;
        public int MachineCode
        {
            get { return machineCode; }
            set { SetProperty(ref machineCode, value); }
        }
        private string operator1;
        public string Operator1
        {
            get { return operator1; }
            set { SetProperty(ref operator1, value); }
        }
        private string messageStr = string.Empty;
        public string MessageStr
        {
            get { return messageStr; }
            set { SetProperty(ref messageStr, value); }
        }
        #endregion
        #region 变量
        private DelegateCommand<object> textBoxLostFocusCommand;
        public DelegateCommand<object> TextBoxLostFocusCommand =>
            textBoxLostFocusCommand ?? (textBoxLostFocusCommand = new DelegateCommand<object>(ExecuteTextBoxLostFocusCommand));

        void ExecuteTextBoxLostFocusCommand(object obj)
        {
            if (obj is string str)
            {
                switch (str)
                {
                    case "LineCode":
                        {
                            using var mdb = new MDbContext(partNum);
                            var param = mdb.MParams.FirstOrDefault(p => p.Name == "LineCode");
                            if (param != null)
                            {
                                param.Value = LineCode.ToString();
                            }
                            mdb.SaveChanges();
                        }
                        break;
                    case "MachineCode":
                        {
                            using var mdb = new MDbContext(partNum);
                            var param = mdb.MParams.FirstOrDefault(p => p.Name == "MachineCode");
                            if (param != null)
                            {
                                param.Value = MachineCode.ToString();
                            }
                            mdb.SaveChanges();
                        }
                        break;
                    case "Operator1":
                        {
                            using var mdb = new MDbContext(partNum);
                            var param = mdb.MParams.FirstOrDefault(p => p.Name == "Operator1");
                            if (param != null)
                            {
                                param.Value = Operator1;
                            }
                            mdb.SaveChanges();
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion
        #region 构造函数
        public ViewAViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            client = new RestClient("http://192.168.1.200:5050");        
            aggregator.ResgiterMessage(arg => {
                var strs = arg.Message.Split(new char[] { ';' });
                switch (strs[0])
                {
                    case "开机":
                    case "复位完成":
                        {
                            try
                            {
                                {
                                    var request = new RestRequest("/api/MES/MachineOEE", Method.Post);
                                    request.AddHeader("Content-Type", "application/json");
                                    request.Timeout = 3000;
                                    OEE oee = new OEE();
                                    oee.lineCode = LineCode;
                                    oee.machineCode = MachineCode;
                                    oee.currentState = "STAY";
                                    oee.conveyor = 0;
                                    request.AddParameter("application/json", JsonConvert.SerializeObject(oee), ParameterType.RequestBody);
                                    var response = client.Execute(request);
                                    addMessage(response.Content);
                                }
                                {
                                    var request = new RestRequest("/api/MES/MachineOEE", Method.Post);
                                    request.AddHeader("Content-Type", "application/json");
                                    request.Timeout = 3000;
                                    OEE oee = new OEE();
                                    oee.lineCode = LineCode;
                                    oee.machineCode = MachineCode;
                                    oee.currentState = "STAY";
                                    oee.conveyor = 1;
                                    request.AddParameter("application/json", JsonConvert.SerializeObject(oee), ParameterType.RequestBody);
                                    var response = client.Execute(request);
                                    addMessage(response.Content);
                                }
                            }
                            catch (Exception ex)
                            {
                                addMessage(ex.Message);
                            }
                        }
                        break;
                    case "急停":
                    case "暂停":
                        {
                            try
                            {
                                {
                                    var request = new RestRequest("/api/MES/MachineOEE", Method.Post);
                                    request.AddHeader("Content-Type", "application/json");
                                    request.Timeout = 3000;
                                    OEE oee = new OEE();
                                    oee.lineCode = LineCode;
                                    oee.machineCode = MachineCode;
                                    oee.currentState = "STOP";
                                    oee.conveyor = 0;
                                    request.AddParameter("application/json", JsonConvert.SerializeObject(oee), ParameterType.RequestBody);
                                    var response = client.Execute(request);
                                    addMessage(response.Content);
                                }
                                {
                                    var request = new RestRequest("/api/MES/MachineOEE", Method.Post);
                                    request.AddHeader("Content-Type", "application/json");
                                    request.Timeout = 3000;
                                    OEE oee = new OEE();
                                    oee.lineCode = LineCode;
                                    oee.machineCode = MachineCode;
                                    oee.currentState = "STOP";
                                    oee.conveyor = 1;
                                    request.AddParameter("application/json", JsonConvert.SerializeObject(oee), ParameterType.RequestBody);
                                    var response = client.Execute(request);
                                    addMessage(response.Content);
                                }
                            }
                            catch (Exception ex)
                            {
                                addMessage(ex.Message);
                            }
                        }
                        break;
                    case "开始工作":
                    case "运行":
                        {
                            try
                            {
                                {
                                    var request = new RestRequest("/api/MES/MachineOEE", Method.Post);
                                    request.AddHeader("Content-Type", "application/json");
                                    request.Timeout = 3000;
                                    OEE oee = new OEE();
                                    oee.lineCode = LineCode;
                                    oee.machineCode = MachineCode;
                                    oee.currentState = "RUN";
                                    oee.conveyor = 0;
                                    request.AddParameter("application/json", JsonConvert.SerializeObject(oee), ParameterType.RequestBody);
                                    var response = client.Execute(request);
                                    addMessage(response.Content);
                                }
                                {
                                    var request = new RestRequest("/api/MES/MachineOEE", Method.Post);
                                    request.AddHeader("Content-Type", "application/json");
                                    request.Timeout = 3000;
                                    OEE oee = new OEE();
                                    oee.lineCode = LineCode;
                                    oee.machineCode = MachineCode;
                                    oee.currentState = "RUN";
                                    oee.conveyor = 1;
                                    request.AddParameter("application/json", JsonConvert.SerializeObject(oee), ParameterType.RequestBody);
                                    var response = client.Execute(request);
                                    addMessage(response.Content);
                                }
                            }
                            catch (Exception ex)
                            {
                                addMessage(ex.Message);
                            }
                        }
                        break;
                    case "UPLOAD":
                        {
                            try
                            {
                                {
                                    var request = new RestRequest("/api/MES/MachineOEE", Method.Post);
                                    request.AddHeader("Content-Type", "application/json");
                                    request.Timeout = 3000;
                                    OEE oee = new OEE();
                                    oee.lineCode = LineCode;
                                    oee.machineCode = MachineCode;
                                    oee.currentState = "STAY";
                                    oee.conveyor = 0;
                                    request.AddParameter("application/json", JsonConvert.SerializeObject(oee), ParameterType.RequestBody);
                                    var response = client.Execute(request);
                                    addMessage(response.Content);
                                }
                                {
                                    var request = new RestRequest("/api/MES/MachineOEE", Method.Post);
                                    request.AddHeader("Content-Type", "application/json");
                                    request.Timeout = 3000;
                                    OEE oee = new OEE();
                                    oee.lineCode = LineCode;
                                    oee.machineCode = MachineCode;
                                    oee.currentState = "STAY";
                                    oee.conveyor = 1;
                                    request.AddParameter("application/json", JsonConvert.SerializeObject(oee), ParameterType.RequestBody);
                                    var response = client.Execute(request);
                                    addMessage(response.Content);
                                }
                            }
                            catch (Exception ex)
                            {
                                addMessage(ex.Message);
                            }
                        }
                        break;
                    default:
                        break;
                }
            },"OEE");
            aggregator.ResgiterMessage(arg =>
            {
                var strs = arg.Message.Split(new char[] { ';' });

                switch (strs[0])
                {
                    case "PartNum":
                        {
                            try
                            {
                                partNum = strs[1];
                                addMessage($"料号:{partNum}");
                                using var mdb = new MDbContext(partNum);
                                var param = mdb.MParams.FirstOrDefault(p => p.Name == "LineCode");
                                if (param != null)
                                {
                                    var r = int.TryParse(param.Value,out var v1);
                                    if (r)
                                    {
                                        LineCode = v1;
                                    }
                                    else
                                    {
                                        LineCode = 1;
                                        param.Value = LineCode.ToString();
                                        param.Type = typeof(int).Name;
                                        mdb.SaveChanges();
                                    }
                                }
                                else
                                {
                                    LineCode = 1;
                                    mdb.MParams.Add(new MParam { Name = "LineCode", Value = LineCode.ToString(), Type = typeof(int).Name, Hand = 0 });
                                    mdb.SaveChanges();
                                }
                                param = mdb.MParams.FirstOrDefault(p => p.Name == "MachineCode");
                                if (param != null)
                                {
                                    var r = int.TryParse(param.Value, out var v1);
                                    if (r)
                                    {
                                        MachineCode = v1;
                                    }
                                    else
                                    {
                                        MachineCode = 2;
                                        param.Value = MachineCode.ToString();
                                        param.Type = typeof(int).Name;
                                        mdb.SaveChanges();
                                    }
                                }
                                else
                                {
                                    MachineCode = 2;
                                    mdb.MParams.Add(new MParam { Name = "MachineCode", Value = MachineCode.ToString(), Type = typeof(int).Name, Hand = 0 });
                                    mdb.SaveChanges();
                                }
                                param = mdb.MParams.FirstOrDefault(p => p.Name == "Operator1");
                                if (param != null)
                                {
                                    Operator1 = param.Value;
                                }
                                else
                                {
                                    Operator1 = "ZhangSan";
                                    mdb.MParams.Add(new MParam { Name = "Operator1", Value = Operator1, Type = typeof(string).Name, Hand = 0 });
                                    mdb.SaveChanges();
                                }                               
                            }
                            catch (Exception ex)
                            {
                                addMessage(ex.Message);
                            }
                        }
                        break;
                    case "进站校验":
                        try
                        {
                            #region 进板
                            #region check
                            {
                                addMessage("CheckInfo");
                                var request = new RestRequest("/api/MES/CheckInfo", Method.Post);
                                request.AddHeader("Content-Type", "application/json");
                                request.Timeout = 3000;
                                MCheck mCheck = new MCheck();
                                mCheck.barCode = strs[1];
                                mCheck.lineCode = LineCode;
                                mCheck.machineCode = MachineCode;
                                mCheck.mac = GetMacAddress("192.168.1.35");
                                mCheck.summarys = new List<Summary>();
                                Summary summary = new Summary();
                                summary.pcsBarcode = strs[1];
                                mCheck.summarys.Add(summary);
                                request.AddParameter("application/json", JsonConvert.SerializeObject(mCheck), ParameterType.RequestBody);
                                var response = client.Execute(request);
                                addMessage(response.Content);
                                aggregator.SendMessage($"CheckInfo;{response.Content}", "MESBack");
                                if (response.Content.ToUpper().Contains("\"RESULT\":0") || response.Content.ToUpper().Contains("\"RESULT\":TRUE"))
                                {
                                    addMessage("CheckEntry");
                                    var request1 = new RestRequest("/api/MES/CheckEntry", Method.Post);
                                    request1.AddHeader("Content-Type", "application/json");
                                    request1.Timeout = 3000;
                                    InOut inOut = new InOut();
                                    inOut.barCode = strs[1];
                                    inOut.machineCode = MachineCode;
                                    inOut.flag = 0;
                                    inOut.mac = GetMacAddress("192.168.1.35");
                                    inOut.detailLists = new List<InOutDetail>();
                                    request1.AddParameter("application/json", JsonConvert.SerializeObject(inOut), ParameterType.RequestBody);
                                    var response1 = client.Execute(request1);
                                    aggregator.SendMessage($"CheckEntry;{response1.Content}", "MESBack");
                                    addMessage(response1.Content);
                                }
                            }
                            #endregion
                         
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            addMessage(ex.Message);
                        }
                        break;
                    case "UpLoad":
                        {
                            try
                            {
                                double Ct = 999;
                                if (double.TryParse(strs[2].Substring(0),out var v1))
                                {
                                    Ct = v1;
                                }
                                List<Comparison> records = JsonConvert.DeserializeObject<List<Comparison>>(strs[1].Substring(0));
                                List<string> barcodes = new List<string>();
                                foreach (var item in records)
                                {
                                    if (!barcodes.Contains(item.条码) && item.条码 != string.Empty)
                                    {
                                        barcodes.Add(item.条码.Substring(0, item.条码.Length));
                                    }
                                }
                                if (barcodes.Count > 0)
                                {
                                    foreach (var bar in barcodes)
                                    {
                                        addMessage($"条码:{bar}");                           
                                        var _records = records.Where(p => p.条码 == bar);
                                        if (_records.Any())
                                        {
                                            var arr = _records.ToArray();
                                            #region save
                                            {
                                                addMessage("SaveProductParamters");
                                                var request = new RestRequest("/api/MES/SaveProductParamters", Method.Post);
                                                request.AddHeader("Content-Type", "application/json");
                                                request.Timeout = 3000;
                                                Save1 save1 = new Save1();
                                                save1.machineCode = MachineCode;
                                                save1.containers = new List<SaveContainer>();
                                                for (int i = 0; i < arr.Length; i++)
                                                {
                                                    SaveContainer saveContainer = new SaveContainer();
                                                    saveContainer.containerName = bar;
                                                    saveContainer.position = i + 1;

                                                    saveContainer.paramters = new List<SaveContainerParamter>();
                                                    SaveContainerParamter saveContainerParamter1 = new SaveContainerParamter();
                                                    saveContainerParamter1.paramName = "Power";
                                                    saveContainerParamter1.currentValue = arr[i].功率.ToString();
                                                    saveContainerParamter1.upperLimitValue = arr[i].功率上限.ToString();
                                                    saveContainerParamter1.lowerLimitValue = arr[i].功率下限.ToString();
                                                    saveContainerParamter1.fixedValue = arr[i].功率标准.ToString();
                                                    saveContainer.paramters.Add(saveContainerParamter1);

                                                    SaveContainerParamter saveContainerParamter2 = new SaveContainerParamter();
                                                    saveContainerParamter2.paramName = "Speed";
                                                    saveContainerParamter2.currentValue = arr[i].速度.ToString();
                                                    saveContainerParamter2.upperLimitValue = arr[i].速度上限.ToString();
                                                    saveContainerParamter2.lowerLimitValue = arr[i].速度下限.ToString();
                                                    saveContainerParamter2.fixedValue = arr[i].速度标准.ToString();
                                                    saveContainer.paramters.Add(saveContainerParamter2);

                                                    SaveContainerParamter saveContainerParamter3 = new SaveContainerParamter();
                                                    saveContainerParamter3.paramName = "Height";
                                                    saveContainerParamter3.currentValue = arr[i].焦距.ToString();
                                                    saveContainerParamter3.upperLimitValue = arr[i].焦距上限.ToString();
                                                    saveContainerParamter3.lowerLimitValue = arr[i].焦距上限.ToString();
                                                    saveContainerParamter3.fixedValue = arr[i].焦距标准.ToString();
                                                    saveContainer.paramters.Add(saveContainerParamter3);

                                                    save1.containers.Add(saveContainer);
                                                }
                                            

                                                request.AddParameter("application/json", JsonConvert.SerializeObject(save1), ParameterType.RequestBody);
                                                var response = client.Execute(request);
                                                addMessage(response.Content);
                                                aggregator.SendMessage($"SaveProductParamters;{response.Content}", "MESBack");
                                            }
                                            #endregion
                                            #region content
                                            {
                                                addMessage("SaveWIResult");
                                                var request = new RestRequest("/api/MES/SaveWIResult", Method.Post);
                                                request.AddHeader("Content-Type", "application/json");
                                                request.Timeout = 3000;
                                                SaveWIResult saveWIResult = new SaveWIResult();
                                                saveWIResult.barCode = bar;
                                                saveWIResult.machineCode = MachineCode;
                                                saveWIResult.paras = new List<SaveWIResultPara>();
                                                
                                                for (int i = 0; i < arr.Length; i++)
                                                {
                                                    SaveWIResultPara saveWIResultPara1 = new SaveWIResultPara();
                                                    saveWIResultPara1.name = "功率";
                                                    saveWIResultPara1.position = i + 1;
                                                    saveWIResultPara1.minValue = arr[i].功率下限;
                                                    saveWIResultPara1.maxValue = arr[i].功率上限;
                                                    saveWIResultPara1.stdValue = arr[i].功率标准;
                                                    saveWIResultPara1.currentValue = arr[i].功率;
                                                    saveWIResultPara1.result = 1;
                                                    saveWIResult.paras.Add(saveWIResultPara1);
                                                }
                                                for (int i = 0; i < arr.Length; i++)
                                                {
                                                    SaveWIResultPara saveWIResultPara1 = new SaveWIResultPara();
                                                    saveWIResultPara1.name = "速度";
                                                    saveWIResultPara1.position = i + 1;
                                                    saveWIResultPara1.minValue = arr[i].速度下限;
                                                    saveWIResultPara1.maxValue = arr[i].速度上限;
                                                    saveWIResultPara1.stdValue = arr[i].速度标准;
                                                    saveWIResultPara1.currentValue = arr[i].速度;
                                                    saveWIResultPara1.result = 1;
                                                    saveWIResult.paras.Add(saveWIResultPara1);
                                                }
                                                for (int i = 0; i < arr.Length; i++)
                                                {
                                                    SaveWIResultPara saveWIResultPara1 = new SaveWIResultPara();
                                                    saveWIResultPara1.name = "焦距";
                                                    saveWIResultPara1.position = i + 1;
                                                    saveWIResultPara1.minValue = arr[i].焦距下限;
                                                    saveWIResultPara1.maxValue = arr[i].焦距上限;
                                                    saveWIResultPara1.stdValue = arr[i].焦距标准;
                                                    saveWIResultPara1.currentValue = arr[i].焦距;
                                                    saveWIResultPara1.result = 1;
                                                    saveWIResult.paras.Add(saveWIResultPara1);
                                                }

                                                request.AddParameter("application/json", JsonConvert.SerializeObject(saveWIResult), ParameterType.RequestBody);
                                                var response = client.Execute(request);
                                                addMessage(response.Content);
                                                aggregator.SendMessage($"SaveWIResult;{response.Content}", "MESBack");
                                            }
                                            #endregion
                                            #region 奕东
                                            {
                                                addMessage("LdrSaveInfo");
                                                var request = new RestRequest("/api/MES/LdrSaveInfo", Method.Post);
                                                request.AddHeader("Content-Type", "application/json");
                                                request.Timeout = 3000;
                                                YDsave yDsave = new YDsave();
                                                yDsave.lineCode = LineCode;
                                                yDsave.machineCode = MachineCode;
                                                yDsave.saveItems = new List<YDsaveItem>();
                                                int result1 = 1;
                                                for (int i = 0; i < arr.Length; i++)
                                                {
                                                    if (arr[i].功率 > arr[i].功率上限 || arr[i].功率 < arr[i].功率下限 ||
                                                    arr[i].速度 > arr[i].速度上限 || arr[i].速度 < arr[i].速度下限 ||
                                                    arr[i].焦距 > arr[i].焦距上限 || arr[i].焦距 < arr[i].焦距下限)
                                                    {
                                                        result1 = 0;
                                                        break;
                                                    }
                                                }
                                                yDsave.saveItems.Add(new YDsaveItem
                                                {
                                                    barCode = bar,
                                                    errorCode = string.Empty,
                                                    erroeMessage = string.Empty,
                                                    remark = "激光数据上报",
                                                    result = result1,
                                                    startTime = DateTime.Now.AddSeconds(Ct * -1),
                                                    endTime = DateTime.Now,
                                                    machineParams = new List<YDmachineParam>()
                                                });
                                                for (int i = 0; i < arr.Length; i++)
                                                {
                                                    int result2 = 1;
                                                    if (arr[i].功率 > arr[i].功率上限 || arr[i].功率 < arr[i].功率下限)
                                                    {
                                                        result2 = 0;                                                     
                                                    }
                                                    yDsave.saveItems[0].machineParams.Add(new YDmachineParam { 
                                                        position = i + 1,
                                                        positionStr = arr[i].焊接点,
                                                        paramName = "功率",
                                                        paramType = "功率",
                                                        currentValue = arr[i].功率,
                                                        mixValue = arr[i].功率下限,
                                                        maxValue = arr[i].功率上限,
                                                        stdValue = arr[i].功率标准,
                                                        result = result2,
                                                        unitName = "W",
                                                        testTime = arr[i].开始时间
                                                    });
                                                }
                                                for (int i = 0; i < arr.Length; i++)
                                                {
                                                    int result2 = 1;
                                                    if (arr[i].速度 > arr[i].速度上限 || arr[i].速度 < arr[i].速度下限)
                                                    {
                                                        result2 = 0;
                                                    }
                                                    yDsave.saveItems[0].machineParams.Add(new YDmachineParam
                                                    {
                                                        position = i + 1,
                                                        positionStr = arr[i].焊接点,
                                                        paramName = "速度",
                                                        paramType = "速度",
                                                        currentValue = arr[i].速度,
                                                        mixValue = arr[i].速度下限,
                                                        maxValue = arr[i].速度上限,
                                                        stdValue = arr[i].速度标准,
                                                        result = result2,
                                                        unitName = "mm/s",
                                                        testTime = arr[i].开始时间
                                                    });
                                                }
                                                for (int i = 0; i < arr.Length; i++)
                                                {
                                                    int result2 = 1;
                                                    if (arr[i].焦距 > arr[i].焦距上限 || arr[i].焦距 < arr[i].焦距下限)
                                                    {
                                                        result2 = 0;
                                                    }
                                                    yDsave.saveItems[0].machineParams.Add(new YDmachineParam
                                                    {
                                                        position = i + 1,
                                                        positionStr = arr[i].焊接点,
                                                        paramName = "焦距",
                                                        paramType = "焦距",
                                                        currentValue = arr[i].焦距,
                                                        mixValue = arr[i].焦距下限,
                                                        maxValue = arr[i].焦距上限,
                                                        stdValue = arr[i].焦距标准,
                                                        result = result2,
                                                        unitName = "mm",
                                                        testTime = arr[i].开始时间
                                                    });
                                                }
                                                //addMessage(JsonConvert.SerializeObject(yDsave));
                                                request.AddParameter("application/json", JsonConvert.SerializeObject(yDsave), ParameterType.RequestBody);
                                                var response = client.Execute(request);
                                                addMessage(response.Content);
                                            }
                                            #endregion
                                            #region 出板
                                            {
                                                addMessage("CheckExit");
                                                var request = new RestRequest("/api/MES/CheckExit", Method.Post);
                                                request.AddHeader("Content-Type", "application/json");
                                                request.Timeout = 3000;
                                                InOut inOut = new InOut();
                                                inOut.barCode = bar;
                                                inOut.machineCode = MachineCode;
                                                inOut.flag = 1;
                                                inOut.mac = GetMacAddress("192.168.1.35");
                                                inOut.detailLists = new List<InOutDetail>();
                                                InOutDetail inOutDetail = new InOutDetail();
                                                inOutDetail.name = "CT";
                                                inOutDetail.value = $"{Ct}";
                                                inOut.detailLists.Add(inOutDetail);
                                                request.AddParameter("application/json", JsonConvert.SerializeObject(inOut), ParameterType.RequestBody);
                                                var response = client.Execute(request);
                                                addMessage(response.Content);
                                                aggregator.SendMessage($"CheckExit;{response.Content}", "MESBack");
                                            }
                                            #endregion
                                        }
                                    }
                                }
                                else
                                {
                                    addMessage("无条码");
                                }
                            }
                            catch (Exception ex)
                            {
                                addMessage(ex.Message);
                            }
                        }
                        break;
                    case "YD进站校验":
                        {
                            try
                            {
                                string[] strs2 = strs[1].Split('|');
                                bool r = false;
                                foreach (string str in strs2)
                                {
                                    if (str != string.Empty)
                                    {
                                        r = true;
                                        break;
                                    }
                                }
                                if (r)
                                {
                                    YDcheck yDcheck = new YDcheck
                                    {
                                        lineCode = LineCode,
                                        machineCode = MachineCode,
                                        vehicleDetails = new List<YDvehicleDetail>()
                                    };
                                    foreach (var str in strs2)
                                    {
                                        if (str != string.Empty)
                                        {
                                            yDcheck.vehicleDetails.Add(new YDvehicleDetail { 
                                                barCode = str
                                            });
                                        }
                                    }
                                    addMessage("LdrCheckIn");
                                    var request = new RestRequest("/api/MES/LdrCheckIn", Method.Post);
                                    request.AddHeader("Content-Type", "application/json");
                                    request.Timeout = 3000;
                                    request.AddParameter("application/json", JsonConvert.SerializeObject(yDcheck), ParameterType.RequestBody);
                                    var response = client.Execute(request);
                                    addMessage(response.Content);
                                }

                            }
                            catch (Exception ex)
                            {
                                addMessage(ex.Message);
                            }
                        }
                        break;
                    default:
                        break;
                }
            }, "MES");
        }
        #endregion

        #region 功能函数
        private void addMessage(string str)
        {
            logger.Debug("MES:" + str);
            string[] s = MessageStr.Split('\n');
            if (s.Length > 1000)
            {
                MessageStr = "";
            }
            if (MessageStr != "")
            {
                MessageStr += "\n";
            }
            MessageStr += DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + str;
        }
        private string GetMacAddress(string ips)
        {
            string rstr = "Null";
            foreach (NetworkInterface n in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (n.OperationalStatus == OperationalStatus.Up)
                {
                    foreach (UnicastIPAddressInformation ip in n.GetIPProperties().UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            if (ip.Address.ToString() == ips)
                            {
                                var mac = n.GetPhysicalAddress().ToString();
                                string macstr = string.Empty;
                                for (int i = 0; i < mac.Length / 2; i++)
                                {
                                    macstr += mac.Substring(i * 2, 2);
                                    if (i < mac.Length / 2 - 1)
                                    {
                                        macstr += "-";
                                    }
                                }
                                rstr = macstr;
                                break;
                            }
                        }
                    }

                }
            }
            return rstr;
        }
        #endregion
    }
    public class InOut
    {
        // "barCode": "BarCode",
        //"userName": "",
        //"machineCode": 1,
        //"mac": "",
        //"testType": "RMG",
        //"product": "",
        //"flag": 0
        public string barCode { get; set; }
        public string userName { get; set; } = string.Empty;
        public int machineCode { get; set; }
        public string mac { get; set; }
        public string testType { get; set; } = "JGHJ";
        public string product { get; set; } = string.Empty;
        public int flag { get; set; }
        public List<InOutDetail> detailLists { get; set; }
    }
    public class InOutDetail
    {
        public string name { get; set; }
        public string value { get; set; }
    }

    public class MCheck
    {
        //    "barCode": "BarCode",
        //      "lineCode": "1",
        //      "machineCode": 1,
        //      "jdeProduct": "",
        //      "side": "22",
        //      "mac": "11",
        //      "carrier": "",
        //summarys:[{

        //        pcsSeq:1,
        //        pcsBarcode:"BarCode",
        //        cover:""

        //        }
        //]
        public string barCode { get; set; }
        public int lineCode { get; set; }
        public int machineCode { get; set; }
        public string jdeProduct { get; set; } = string.Empty;
        public string side { get; set; } = "22";
        public string mac { get; set; }
        public string carrier { get; set; } = string.Empty;
        public List<Summary> summarys { get; set; }
    }
    public class Summary
    {
        public string pcsSeq { get; set; } = "1";
        public string pcsBarcode { get; set; }
        public string cover { get; set; } = string.Empty;
    }
    public class Save1
    {
        public string fixtureName { get; set; } = string.Empty;
        public int machineCode { get; set; }
        public List<SaveContainer> containers { get; set; }
    }
    public class SaveContainer
    {
        public string containerName { get; set; }
        public int position { get; set; } = 0;
        public int isPass { get; set; } = 0;
        public List<SaveContainerParamter> paramters { get; set; }
    }
    public class SaveContainerParamter
    {
        public string paramName { get; set; }
        public string currentValue { get; set; }
        public string upperLimitValue { get; set; }
        public string lowerLimitValue { get; set; }
        public string fixedValue { get; set; }
    }
    public class SaveWIResult
    {
        //{
        //  "barCode": "string",
        //  "machineCode": 0,
        //  "paras": [
        //    {
        //      "position": 0,
        //      "minValue": 0,
        //      "maxValue": 0,
        //      "stdValue": 0
        //    }
        //  ]
        //}
        public string barCode { get; set; }
        public int machineCode { get; set; }
        public List<SaveWIResultPara> paras { get; set; }

    }

    public class OEE
    {
        //           "lineCode": 1,
        //       "machineCode": 2,
        // errorMsg:'',
        // errorCode:'',
        //currentState:"RUN/",
        //       "conveyor": 0
        public int lineCode { get; set; }
        public int machineCode { get; set; }
        public string errorMsg { get; set; } = string.Empty;
        public string errorCode { get; set; } = string.Empty;
        public string currentState { get; set; }//RUN STOP ERROR STAY//
        public int conveyor { get; set; }
    }
    public class SaveWIResultPara
    {
        public string name { get; set; }
        public int position { get; set; }
        public double minValue { get; set; }
        public double maxValue { get; set; }
        public double stdValue { get; set; }
        public double currentValue { get; set; }
        public int result { get; set; }
    }
    public class YDcheck
    {
        public int lineCode { get; set; }
        public int machineCode { get; set; }
        public List<YDvehicleDetail> vehicleDetails { get; set; }
    }
    public class YDvehicleDetail
    {
        public string barCode { get; set; }
    }
    public class YDsave
    {
        //"lineCode": 1,
        //"machineCode": 5,
        //"saveItems": 
        public int lineCode { get; set; }
        public int machineCode { get; set; }
        public List<YDsaveItem> saveItems { get; set; }
    }
    public class YDsaveItem
    {
        //"barCode": "ydbc001",
        //"errorCode": "",
        //"erroeMessage": "",
        //"remark": "激光数据上报",
        //"result": 1,
        //"startTime": "2023-09-17T06:13:19.206Z",
        //"endTime": "2023-09-17T06:13:19.206Z",
        //"machineParams"
        public string barCode { get; set; }
        public string errorCode { get; set; }
        public string erroeMessage { get; set; }
        public string remark { get; set; }
        public int result { get; set; }
        public DateTime startTime { get; set; }
        public DateTime endTime { get; set; }
        public List<YDmachineParam> machineParams { get; set; }
    }
    public class YDmachineParam
    {
        //"position": 1,
        //"positionStr": "功率点位1",
        //"paramName": "功率",
        //"paramType": "功率",
        //"currentValue": 457.0,
        //"mixValue": 456.0,
        //"maxValue": 458.0,
        //"stdValue": 457.0,
        //"result": 1,
        //"unitName": "度",
        //"testTime": "2023-09-17T06:13:19.206Z"
        public int position { get; set; }
        public string positionStr { get; set; }
        public string paramName { get; set; }
        public string paramType { get; set; }
        public double currentValue { get; set; }
        public double mixValue { get; set; }
        public double maxValue { get; set; }
        public double stdValue { get; set; }
        public int result { get; set; }
        public string unitName { get; set; }
        public DateTime testTime { get; set; }
    }
}
